package problem_everyday;

public class D2025_07_10_P3440 {
    /*
    * 只允许移动会议一次，仍然求相邻两次会议之间的空闲时间最长是多少
    * 但允许改变会议的顺序
    * */
    public int maxFreeTime(int eventTime, int[] startTime, int[] endTime) {
        int last_gap=0, cur_gap;
        int n = startTime.length;
        int left, right;
        int res = 0;
        int[] gaps = new int[n+1];
        for(int i=0; i<=n; i++){
            if(i==0){
                left = 0;
            } else {
                left = endTime[i-1];
            }

            if(i==n){
                right = eventTime;
            } else {
                right = startTime[i];
            }

            gaps[i] = right - left;
        }
        int[] pre_max = new int[n];
        int[] after_max = new int[n];
        pre_max[0] = 0;
        after_max[n-1] = 0;
        for(int i=1; i<n; i++){
            pre_max[i] = Math.max(pre_max[i-1], gaps[i-1]);
            after_max[n-i-1] = Math.max(after_max[n-i], gaps[n-i+1]);
        }
        for(int i=0; i<n; i++){
            int gap_sum = gaps[i] + gaps[i+1];
            int meeting_time = endTime[i] - startTime[i];
            if(pre_max[i] >= meeting_time || after_max[i] >= meeting_time){
                gap_sum += meeting_time;
            }
            res = Math.max(res, gap_sum);
        }
        return res;
    }
}
